class Entity < ActiveRecord::Base
	#create stream relationship
	has_one :stream, :dependent => :destroy
	
	#is friends relationship
	has_many :friendships
	has_many :friends, :through => :friendships
	
	#sends and receives private popsicle relationships
	has_many :private_popsicles
	
	#part of clique relationship
	#has_many :partofcliques
	#has_many :cliques, :through => :partofcliques
	#edits multiple cliques relationship
	#has_many :cliques
	
	validates :name, :presence => true	
	validates :type, :presence => true	
	after_create :ensure_stream_is_created
	
	def ensure_stream_is_created
		stream = Stream.create(:entity_id => self.id)		
	end
	
	def find_friends		
		entities = []
		Friendship.all.each do |f|
			if f.accepted?
				if f.entity_id == self.id
					entities << Entity.find(f.friend_id)
				elsif f.friend_id == self.id
					entities << Entity.find(f.entity_id)
				end				
			end
		end	
		return entities
	end
	
	def friends_stream
	
		friends = find_friends
		
		messages = []
		
		friends.each do |f|
			messages << f.stream.find_popsicles
	
	end
	
	return messages;
	
	
	end
	
end
